﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace Common
{
    [XmlRoot("RuleSet", IsNullable = false, Namespace = "")]
    public class RuleSet
    {
        [XmlIgnore()]
        public static readonly string Story = "Story", Talkback = "Talkback", Blog = "Blog", Forum = "Forum", Uri = "Uri";
        public string Name;
        public string DisplayName;
        public List<string> ContentUris = new List<string>(); //A list of the uris where this type of content can be found on the site.
        public List<string> IdentifyingUris = new List<string>(); //A partial URL Identifying this content type in the site.
        public List<string> XmlUris = new List<string>();//Partial URL identifying an RSS feed/XML page
        public List<Rule> Rules = new List<Rule>();
        public TalkbackRule TalkbackRule;
        public string Encoding;
        public int CrawlDepth = 0;
        public string GenericDisplayUrl = string.Empty;

        public RuleSet(string name, List<Rule> rules)
        {
            Init(name, rules);
        }
        public RuleSet()
        {
            Init(string.Empty, new List<Rule>());
        }

        private void Init(string name, List<Rule> rules)
        {
            Name = name;
            Rules = rules;
            TalkbackRule = new TalkbackRule();
        }

        public Rule this[string ruleName]
        {
            get
            {
                foreach (Rule rule in Rules)
                { if (rule.Name == ruleName) return rule; }
                return null;
            }
            set
            {
                foreach (Rule rule in Rules)
                { if (rule.Name == ruleName) { Rules.Remove(rule); break; } }
                value.Name = ruleName;
                Rules.Add(value);
            }
        }

        public Document.Type GetDocType()
        {
            return (Name.Equals(Blog)) ? Document.Type.Blog : Document.Type.Story;

        }
    }
}
